package www.openteam.com.ui.realtime;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cundong.recyclerview.EndlessRecyclerOnScrollListener;
import com.cundong.recyclerview.HeaderAndFooterRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import www.openteam.com.R;
import www.openteam.com.adapter.StockAdapter;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.ProStock;
import www.openteam.com.contract.realtime.StockContract;
import www.openteam.com.presenter.realtime.StockPresenter;
import www.openteam.com.tool.RecyclerViewStateUtils;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.ui.DetailedActivity;
import www.openteam.com.widget.LoadingFooter;

/**
 * Created by kylin on 2017/1/5.
 * 库存查询1
 */

public class StockFragment extends mBaseFragment<StockPresenter> implements
        StockContract.StockView {


    @Bind(R.id.ed_1)
    EditText ed_1;
    @Bind(R.id.tv_1)
    TextView tv_1;
    @Bind(R.id.query_btn)
    Button query_btn;

    View view;
    private ArrayList<ProStock.DataEntity> stocks;          //送货进度实体类
    private static int pageSize = 0;
    private final String TAG = StockFragment.class.getSimpleName();
    int i = 0;
    private HeaderAndFooterRecyclerViewAdapter mFooterRecyclerViewAapter = null;
    private StockAdapter stockAdapter = null;
    Throwable throwable = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        detailGather = ((DetailedActivity) getActivity()).getDetailGather();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tv_1.setText(detail[0]);

        initSRView(view);
        mRecyclerView.addOnScrollListener(mOnScrollListener);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        measurementLayoutHeight();
    }

    @Override
    protected void initView() {
        query_btn.setOnClickListener(new View.OnClickListener() {  //查询按钮
            @Override
            public void onClick(View v) {
                createProgressDialog("正在查询,请稍候!");
                if (stockAdapter == null) {
                    stocks = new ArrayList<ProStock.DataEntity>();
                    mPresenter.queryStock(getRdsno(), getStype(), pageSize);
                } else {
                    stockAdapter.deleteItems(stocks);
                    throwable = null;
                    i = 0;
                    stocks.clear();
                    onRefresh();
                }
            }
        });
    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected StockPresenter loadPresenter() {
        return new StockPresenter();
    }


    @Override
    public String getRdsno() {
        return ed_1.getText().toString();
    }

    @Override
    public String getStype() {
        String stype = mySetting.getString("rCode", "");
        Log.d(TAG, "getStype: " + stype);
        return stype;
    }

    @Override
    public void querySSuccess(List<ProStock.DataEntity> proStock) {
        mProgressDialog.dismiss();
        for (ProStock.DataEntity dev : proStock) {
            stocks.add(dev);
        }
        setProduceScheduleAdapter();
//        if (arrow_statue) {
//            touchEvent();
//        }
    }

    @Override
    public void querySFail(String failMsg) {
        mProgressDialog.dismiss();
        this.throwable = new Throwable();
        if (failMsg.substring(failMsg.indexOf(":")+1).equals("暂无合适的数据")
                ) {
            RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
            ToastUtil.showToast(getActivity(), failMsg.substring(failMsg.indexOf(":")+1));
        } else {
            RecyclerViewStateUtils.setFooterViewState(getActivity(), this.mRecyclerView, REQUEST_COUNT, LoadingFooter.State.NetWorkError, this.mFooterClick);
            ToastUtil.showToast(getActivity(), "网络连接有误");
        }
    }

    private void setProduceScheduleAdapter() {
        if (stockAdapter == null) {
            stockAdapter = new StockAdapter(getActivity());
            stockAdapter.addItems(stocks);
            mFooterRecyclerViewAapter = new HeaderAndFooterRecyclerViewAdapter(stockAdapter);
            mRecyclerView.setAdapter(mFooterRecyclerViewAapter);
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        } else {
            mFooterRecyclerViewAapter.notifyDataSetChanged();
            stockAdapter.addItems(stocks);
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        }
    }

    @Override
    public void onRefresh() {
        if ((mRecyclerView.getChildCount() > 0) && (!getRdsno().isEmpty())) {
            Log.d(TAG, "onRefresh: " + mRecyclerView.getChildCount());
            mRecyclerView.removeAllViews();
            stocks.clear();
            stockAdapter = null;
            mFooterRecyclerViewAapter = null;
            mPresenter.queryStock(getRdsno(), getStype(), 0);
            throwable = null;
            i = 0;
            mSwipeRefresh.setRefreshing(false);
        } else if ((mRecyclerView.getChildCount() <= 0) && (!getRdsno().isEmpty())) {
            Log.d(TAG, "onRefresh: else if " + mRecyclerView.getChildCount());
            mRecyclerView.removeAllViews();
            stockAdapter = null;
            mFooterRecyclerViewAapter = null;
            stocks.clear();
            mPresenter.queryStock(getRdsno(), getStype(), 0);
            mSwipeRefresh.setRefreshing(false);
        }
    }


    public EndlessRecyclerOnScrollListener mOnScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
            Log.d(TAG, "onLoadNextPage: yx:" + (throwable != null) + ":" + (stocks.size() % 10 == 0));
            if (state == LoadingFooter.State.Loading) {
                i++;
            }
            i++;
            if (throwable == null && (stocks.size() % 10 == 0)) {


                stocks = new ArrayList<ProStock.DataEntity>();
                mPresenter.queryStock(getRdsno(), getStype(), i);
                RecyclerViewStateUtils.
                        setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            } else if (throwable == null && (stocks.size() % 10 != 0)) {
                //the end
                Log.d(TAG, "onLoadNextPage:  !-9 ");
                mPresenter.queryStock(getRdsno(), getStype(), i);
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            } else if (throwable != null) {
                //the end
                i = 0;
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            }

        }
    };


    private View.OnClickListener mFooterClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            mPresenter.queryStock(getRdsno(), getStype(), i);
        }
    };

}
